<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="RolesList">
              <el-table-column label="序号" width="50" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <!-- 渲染这一列不要刘宽度 -->
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="mini" type="primary" @click="hEdit(row.id)">编辑</el-button>
                  <el-button type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹窗窗口 -->
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit?'编辑':'新增'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="hclose">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, addRoles, getRolesDetail, updateRoles, delRoles } from '@/api/settings'
export default {
  data() {
    return {
      isEdit: false, // 是否是编辑
      roleForm: {
        name: '',
        description: ''
      }, // 表单
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }, {
          min: 2, max: 10, message: '请输入2到10位的名称', trigger: 'blur'
        }]

      }, // 表单校验
      showDialog: false, // 先隐藏添加窗口
      RolesList: [], // 获取所有角色的列表
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0 // 总的数据调数
    }
  },

  // 计算属性
  computed: {
    maxNum() {
      return Math.ceil(this.total / this.pageParams.pagesize)
    },
    // 最后一页是不是满的
    isLastPageFulled: function() { return this.total % this.pageParams.pagesize === 0 }

  },
  created() {
    this.loadgetRoles()
  },
  methods: {
    // 删除功能
    async  hDel(id) {
      const res = await delRoles(id)
      console.log('hDel', res)
      this.loadgetRoles()
    },
    // 点击取消按钮
    hclose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 编辑更新
    async doEidt() {
      try {
        const res = await updateRoles(this.roleForm)
        console.log('updateRoles', res)
        this.loadgetRoles()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 获取某一详情
    async loadgetRolesDetail(id) {
      const res = await getRolesDetail(id)
      console.log('loadgetRolesDetail', res)
      this.roleForm = res.data
    },
    //
    hAdd() {
      this.isEdit = false // 不是编辑
      this.showDialog = true
    },
    // 编辑表单
    hEdit(id) {
      // 1. 把当前的数据直接给表单,封装回显的表单
      this.loadgetRolesDetail(id)
      this.isEdit = true
      this.showDialog = true
    },
    // 表单添加
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return this.$message.error('新增失败') // 失败

        this.isEdit ? this.doEidt() : this.doAdd()

        //
      })
    },
    // 实现新增功能
    async doAdd() {
      try {
        const res = await addRoles(this.roleForm)

        if (res.code !== 10000) this.$message.error(res.message) // 失败
        this.$message.success(res.message) // 添加成功
        if (this.isLastPageFulled) {
          // 满的,最大页加一
          this.pageParams.page = this.maxNum + 1
          //
          this.total = this.total + 1
        } else {
          // 跳转到最大页
          this.pageParams.page = this.maxNum
        }

        // 重新渲染,需要在这里,跳转才正确
        this.loadgetRoles()
        // console.log('roleForm', res)
      } catch (error) {
        console.log(error)
      }
      this.hclose()
    },

    // 分页功能
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // 当前页数
      this.pageParams.page = 1
      this.pageParams.pagesize = val
      //
      this.loadgetRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      // 总共有多少页
      this.pageParams.page = val
      this.loadgetRoles()
    },

    // 获取所有角色
    async loadgetRoles() {
      const res = await getRoles(this.pageParams)
      console.log('loadgetRoles', res)
      this.RolesList = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
